package com.example.juc.controller;

import java.util.concurrent.TimeUnit;

/**
 * @author jl
 * @since 2021/9/26 22:56
 */
public class DeadLockDemo {
    static Object lockA = new Object();
    static Object lockB = new Object();

    public static void main(String[] args) {
        Thread success = new Thread(() -> {
            synchronized (lockA) {
                // 让当前线程睡眠，保证让另一线程得到锁进入同步方法
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockB) {
                    System.out.println("success");
                }
            }
        });
        success.interrupt();
        new Thread(() -> {
            synchronized (lockB) {
                synchronized (lockA) {
                    System.out.println("success");
                }
            }
        }).start();
    }
}
